#include "touch_screen.h"

// extern struct input_event buf;
int touch_fd = -1;
int touch_x;
int touch_y;

// 初始化x,y

void reset_xy(void)
{
    touch_x = 0;
    touch_y = 0;
}

void get_xy(int *x, int *y)
{
    // 打开触摸屏
    touch_fd = open("/dev/input/event0", O_RDONLY);
    if (touch_fd < 0)
    {
        perror("open() touch screeen fail");
        return;
    }
    struct input_event buf;
    while (1)
    {
        bzero(&buf, sizeof(buf));
        read(touch_fd, &buf, sizeof(buf));

        if (buf.type == EV_KEY && buf.code == BTN_TOUCH)
        {
            // printf("%s\n", buf.value==0 ? "松开":"按下");
            if (buf.value == 0)
                break;
        }
        if (buf.type == EV_ABS && buf.code == ABS_X)
        {
            *x = buf.value * 800 / 1024; // 黑色边框屏幕
        }
        if (buf.type == EV_ABS && buf.code == ABS_Y)
        {
            *y = buf.value * 480 / 600; // 黑色边框屏幕
        }
    }
    close(touch_fd);
}

// 线程一直检测屏幕点击

void *read_touch_screeninfo(void *arg)
{

    while (1)
    {

        get_xy(&touch_x, &touch_y);
    }
}
